<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像 -->
      <div class="avator">
        <img src="@/assets/logo.png" alt="" />
      </div>
      <!-- 表单 -->
      <el-form
        :model="loginformData"
        :rules="loginformRules"
        label-width="0px"
        class="formLogin"
        ref="loginformRef"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            v-model="loginformData.username"
            prefix-icon="iconfont icon-yonghu"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="loginformData.password"
            prefix-icon="iconfont icon-mima"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { getLoginInfo } from '@/request/api.js'
export default {
  name: 'Login',
  data() {
    return {
      // 表单的数据绑定对象
      loginformData: {
        username: 'admin',
        password: '123456',
      },
      // 验证规则
      loginformRules: {
        username: [
          { required: true, message: '请输入登录名称', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '长度在 3 到 10 个字符',
            trigger: 'blur',
          },
        ],
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          {
            min: 6,
            max: 15,
            message: '长度在 6 到 15 个字符',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  methods: {
    // 表单重置
    resetForm() {
      this.$refs.loginformRef.resetFields()
    },
    // 封装一个验证登录的axios请求
    login() {
      // 对表单进行预先校验
      this.$refs.loginformRef.validate(async (vaild) => {
        if (!vaild) {
        }
        // 成功就发起axios请求
        const { data: res } = await getLoginInfo(this.loginformData)
        // console.log(res)
        if (res.meta.status !== 200) return this.$message.error('登录失败！')
        this.$message.success('登录成功！')
        // 登录成功之后先保存token在客户端的sessionStorage中
        //  1.除了登录之外的接口，必须在登录之后才能访问
        //  2.token只在浏览器打开生效
        sessionStorage.setItem('token', res.data.token)
        // 编程式路由跳转
        // console.log(this.$router)
        this.$router.push('/home')
      })
    },
  },
}
</script>

<style lang="less" scoped>
.login_container {
  height: 100%;
  background-color: rgb(15, 48, 92);
  .login_box {
    width: 450px;
    height: 300px;
    border-radius: 3px;
    background-color: #fff;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .avator {
      width: 130px;
      height: 130px;
      border: 1px solid rgb(167, 163, 163);
      border-radius: 50%;
      padding: 10px;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      background-color: #fff;
      box-shadow: 0 0 10px #fff;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: rgb(196, 186, 186);
      }
    }
  }
}
.formLogin {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
